import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Microsoft
 * Date: 2023-03-02
 * Time: 14:34
 */
class MySingleList {
    static class ListNode {
        public int val;//存储的数据
        public ListNode next;//存储下一个节点的地址

        public ListNode(int val){
            this.val = val;
            //由于下一个节点的地址，我们不能提前知道，所以我们不用赋值Node
        }
    }
    public ListNode head;//代表当前链表的头节点的引用

    public void creatLink() {
        ListNode listNode1 = new ListNode(12);
        ListNode listNode2 = new ListNode(12);
        ListNode listNode3 = new ListNode(12);
        ListNode listNode4 = new ListNode(12);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        head = listNode1;
    }
    /**
     * 遍历链表
     */
    public void display() {
        //如果说 把整个链表遍历完成那么就需要head == null 这个时候head指向最后一个节点后的null
        //如果说 比哪里到链表的最后剩一个尾巴 那么就head.next == null 这个时候head指向最后一个节点，但是经过判定后退出
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //得到单链表的长度 时间复杂度0(N)
    public int size() {
        int count = 0;
        ListNode cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //头插法 时间复杂度：O(1)
    public void addFirst(int data){
        ListNode listNode = new ListNode(data);
        listNode.next = head;//如果没有节点，那么head = null 同样适用
        head = listNode;
    }

    //尾插法 空间复杂度：O(N) 因为包含找尾巴的过程
    public void addLast(int data){
        ListNode listNode = new ListNode(data);
        //判断是否存在节点，如果没有节点，直接head = node 就行了
        if (head == null) {
            head = listNode;
            return;
        }
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = listNode;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data) throws ListIndexOutOfException{
        checkIndex(index);
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        ListNode cur = findIndexSubOne(index);
        ListNode listNode = new ListNode(data);
        listNode.next = cur.next;
        cur.next = listNode;

    }

    /**
     * 找到index - 1位置的节点的地址
     * @param index
     * @return
     */
    private ListNode findIndexSubOne(int index) {
        ListNode cur = head;
        int count = 0;
        while (count != index - 1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

    private void checkIndex(int index) {
        if (index < 0 || index > size()) {
            throw new ListIndexOutOfException("index位置不合法");
        }
    }

    //删除第一次出现关键字为key的节点 时间复杂度：O(N)
    public void remove(int key){
        //这里必须先判断是否为空指针，否则在后面会造成空指针异常
        if (head == null) {
            return;//一个节点都没有
        }
        //判断头节点的特殊情况，尾节点不算特殊情况
        if(head.val == key) {
            head = head.next;
            return;
        }
        ListNode cur = searchPrev(key);

        ListNode del = cur.next;//记录要删除的节点
        cur.next = cur.next.next;
    }

    /**
     * 找到关键字key的前一个节点
     * @param key
     */
    private ListNode searchPrev(int key) {
        if (head == null) {
            return null;//一个节点都没有
        }
        ListNode cur = head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;//代表没有你要删除的节点
    }
    //删除所有值为key的节点 要求遍历一遍将所有的删掉
    public void removeAllKey(int key) {
        if (head == null) {
            return;
        }
        /*        while (head.val == key) {
            head = head.next;                //一种方式处理头节点没有进行检测的问题
        }*/
        ListNode prev = head;
        ListNode cur = head.next;

        while (cur != null) {

            if (cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }

            if (head.val == key) {
                head = head.next;            //另一种方式处理头节点问题
            }
        }
    }
    public void clear() {
        head = null;
    }

    /**
     * 将链表进行逆置
     * @param head
     * @return
     */
    public ListNode reverseList(ListNode head) {
        if (head == null) {
            return null;
        }
        //说明此时的链表当中只有一个节点，如果进行链表的逆置不发生变化
        if (head.next == null) {
            return head;
        }
        ListNode cur = head.next;//让cur指向第二个节点
        head.next = null;//这个时候第一个节点就不指向第二个节点了

        while (cur != null) {
            ListNode curNext = cur.next;
            //头插法，插入cur
            cur.next = head;
            head = cur;
            cur =curNext;
        }
        return head;

    }

    /**
     * 查找一个链表的中间节点，如果中间节点是两个，那么选择后者为中间节点
     * @param head
     * @return
     */
    public ListNode middleNode(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    /**
     * 输出该链表当中倒数第k个节点
     * @param head
     * @param k
     * @return
     */
    public ListNode findKthToTail(ListNode head,int k) {
        if (k <= 0 || head == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        //1.fast走k-1步
        while (k-1 != 0) {
            fast = fast.next;
            if (fast == null) {
                return null;
            }
            k--;
        }

        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

    public boolean chkPalindrome() {
        if (head == null) {
            return false;
        }
        if (head.next == null) {
            return true;
        }
        ListNode fast = head;
        ListNode slow = head;
        //1.找中间节点
        while (fast != null && fast.next != null) {
            //fast != null && fast.next != null分别判断对应的偶数节点和奇数节点
            fast = fast.next.next;
            slow = slow.next;
        }
        //2.翻转
        ListNode cur = slow.next;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //3.一个从头往后 一个从头往前
        while (slow != head) {
            if (head.val != slow.val) {
                return false;
            }
            //这种是偶数的情况下需要进行特殊的判断，不然的话永远都是返回false，因为slow和head不会相遇
            if (head.next == slow) {
                return true;
            }
            slow = slow.next;
            head = head.next;
        }
        return true;
    }

    /**
     * 将链表进行分割，前者所有节点小于x，后者所有节点都大于等于x，且相对顺序不发生改变
     * @param pHead
     * @param x
     * @return
     */
    public ListNode partition(ListNode pHead, int x) {
        ListNode bs = null;//before start
        ListNode be = null;//before end
        ListNode as = null;//after start
        ListNode ae = null;//after end

        ListNode cur = head;
        while (cur != null) {
            if (cur.val < x) {
                if (bs == null) {
                    bs = cur;
                    be = cur;
                }else {
                    be.next = cur;
                    be = be.next;
                }
            }else {
                if (as == null) {
                    as = cur;
                    ae = cur;
                }else {
                    ae.next = cur;
                    ae = ae.next;
                }

            }
            cur = cur.next;
        }
        //有可能同时都是小于x 或者 大于等于x
        if (bs == null) {
            return as;//如果bs为空 那么返回的as无论是否为空都可以
        }
        //说明此时的bs不为空
        be.next = as;
        if (as != null) {
            ae.next = null;
        }
        return bs;
    }

    public boolean hasCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断入环的节点
     * @param head
     * @return
     */
    public ListNode detectCycle(ListNode head) {
        //首先判断链表是否有环
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                break;
            }
        }
        if (fast == null || fast.next == null) {
            return null;
        }
        //当代码走到这里的时候，说明是有环的。
        slow = head;
        while (slow != fast) {
            slow = slow.next;
            fast = fast.next;
        }
        return fast;
    }

}


















